package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.entity.RolePermission;
import com.entity.User;
import com.entity.UserPermission;
import com.service.RolePermissionService;
import com.service.UserPermissionService;
import com.service.UserService;
import com.util.ReturnResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/api")
public class UserController {
    @Resource
    private UserService service;
    
    @Resource
    private UserPermissionService userPermissionService;
    
    @Resource
    private RolePermissionService rolePermissionService;
    
    @RequestMapping("/user/login/{username}/{password}")
    @ResponseBody
    public ReturnResult login(@PathVariable("username") String username, @PathVariable("password") String password, HttpSession session){
        ReturnResult result = new ReturnResult();
        User user = service.login(username, password);
        if(user != null) {
            result.returnSuccess(user,"登录成功！");
            session.setAttribute("user",user);
        }else{
            result.returnFail("登陆失败！");
        }         
        return result;
    }
    
    @RequestMapping("/user/logout")
    @ResponseBody
    public ReturnResult logout(HttpSession session){
        //清空session
        session.invalidate();
        return new ReturnResult().returnSuccess(null,"退出成功！");
    }
    
    @RequestMapping("/user/selectList/{page}/{limit}")
    @ResponseBody
    public ReturnResult selectList(@PathVariable("page") int page,@PathVariable("limit") int limit){
        ReturnResult result = new ReturnResult();
        Page<User> pageConfig = new Page<>(page,limit);
        pageConfig.setTotal(service.selectCount(null));
        Page<User> userPage = service.selectPage(pageConfig,null);
        result.returnSuccess(userPage,"查询成功！");
        return result;
    }
    
    @RequestMapping("/user/selectOne/{id}")
    @ResponseBody
    public ReturnResult selectOne(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        User user = service.selectById(id);
        result.returnSuccess(user,"查询成功！");
        return result;
    }
    
    @RequestMapping("/user/add")
    @ResponseBody
    public ReturnResult add(@RequestBody User userData){
        ReturnResult result = new ReturnResult();
        boolean addResult = service.insert(userData);
        if(addResult) 
            result.returnSuccess(addResult,"新增成功！");
        else
            result.returnFail("新增失败！");
        return result;
    }
    
    @RequestMapping("/user/upd")
    @ResponseBody
    public ReturnResult upd(@RequestBody User userData){
        ReturnResult result = new ReturnResult();
        boolean addResult = service.updateById(userData);
        if(addResult){
            result.returnSuccess(addResult,"修改成功！");
        }else{
            result.returnFail("修改失败！");
        }
        return result;
    }
    
    @RequestMapping("/user/del/{id}")
    @ResponseBody
    public ReturnResult del(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        boolean result_Del_Permission = userPermissionService.delete(new EntityWrapper<UserPermission>().eq("userId",id));
        if(result_Del_Permission){
            boolean delResult = service.deleteById(id);
            if(delResult)
                result.returnSuccess(delResult,"删除成功！");
            else
                result.returnFail("删除权限成功，删除用户失败！");
            return result;
        }else{
            result.returnFail("删除失败！");
        }
        return result;
    }
    
    @RequestMapping("/user/selectPermission/{id}")
    @ResponseBody
    public ReturnResult selectPermission(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        User user = service.selectById(id);
        if(userPermissionService.selectCount(new EntityWrapper<UserPermission>().eq("userId",id)) > 0){
            result.returnSuccess(userPermissionService.selectList(new EntityWrapper<UserPermission>().eq("userId",id)),"查询成功！");
        }else {
            result.returnSuccess(rolePermissionService.selectList(new EntityWrapper<RolePermission>().eq("roleId",user.getRoleId())),"查询成功！");
        }
        return result;
    }
    
    
    @RequestMapping("/user/doAssign/{id}/{arrStr}")
    @ResponseBody
    public ReturnResult doAssign(@PathVariable("id") int id,@PathVariable("arrStr") String arrStr){
        ReturnResult result = new ReturnResult();
        String cutStr = ",";
        String[] newStr = arrStr.split(cutStr);
        boolean delResult = userPermissionService.delete(new EntityWrapper<UserPermission>().eq("userId",id));
        if(delResult){
            boolean addResult = false;
            for(String str:newStr){
                int i = Integer.parseInt(str);
                addResult = userPermissionService.insert(new UserPermission(id,i));
            }
            result.returnSuccess(addResult,"权限分配成功！");
        } else{
            result.returnFail("权限分配失败！");
        } 
        return result;
    }
}
